package asapIs134CK.womi.client.games.logoGame.froggitTheFrog;

import asapIs134CK.womi.client.games.logoGame.utils.Position;
import asapIs134CK.womi.client.games.logoGame.utils.SimplePositionTraker;
import asapIs134CK.womi.client.games.logoGame.utils.exceptions.ValueException;
import asapIs134CK.womi.client.imgs.logoGame.LogoGameImageBundle;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Image;


/**
 * represents the heros of our game
 * @author LeoJ
 *
 */
public class FroggitMyFrog {
	private SimplePositionTraker _position;
	private static ImageResource imgCreator = 
		((LogoGameImageBundle) GWT.create(LogoGameImageBundle.class)).froggit_small();
	
	public FroggitMyFrog(Position start){
		_position = new SimplePositionTraker(start);
	}
	
	public void moveToAPoint(Position newPosition){
		this._position.setPosition(newPosition);
		//TODO See if we should use something like an update here.
	}
	
	public Position getPosition(){
		return new Position(this._position.getPosition());
	}
	
	private void moveToRight(){
		try {
			this._position.setX(_position.getX()+1);
		} catch (ValueException e) {
			System.out.println("forbidden move : we stay where we are");
		}
	}
	private void moveToLeft(){
		try {
			this._position.setX(_position.getX()-1);
		} catch (ValueException e) {
			System.out.println("forbidden move : we stay where we are");
		}
	}
	private void moveToTop(){
		try{
			this._position.setY(_position.getY()-1);
		}catch (ValueException e) {
			System.out.println("forbidden move : we stay where we are");
		}
	}
	private void moveToDown(){
		try{
			this._position.setY(_position.getY()+1);
		}catch (ValueException e) {
			System.out.println("forbidden move : we stay where we are");
		}
	}
	
	public void applyAction(FroggitAction action){
		switch (action) {
		case MoveToDown:
			this.moveToDown();
			break;
		case MoveToTop:
			this.moveToTop();
			break;
		case MoveToLeft:
			this.moveToLeft();
			break;
		case MoveToRight:
			this.moveToRight();
			break;
		case Wait:
		default:
			break;
		}
	}

	public void returnToPreviousPosition() {
		_position = new SimplePositionTraker(_position.getPrevious());
	}

	public Position getPreviousPosition() {
		return _position.getPrevious();
	}
	
	public Image getImage(){
		return new Image(imgCreator);
	}
}
